package com.bmob.lover.ui.fragment.festival

import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.RecyclerView
import com.qmuiteam.qmui.skin.QMUISkinHelper
import com.theone.common.constant.BundleConstant
import com.theone.common.ext.*
import com.theone.common.widget.datePicker.DateTimePicker
import com.bmob.lover.R
import com.bmob.lover.app.ext.toJson
import com.bmob.lover.data.model.bean.Background
import com.bmob.lover.data.model.bean.TheFile
import com.bmob.lover.databinding.FragmentFestivalBgAddBinding
import com.bmob.lover.ui.fragment.base.BaseFileUploadFragment
import com.bmob.lover.viewmodel.FestivalBgAddViewModel
import com.bmob.lover.viewmodel.base.BaseBmobActionViewModel
import com.bmob.lover.viewmodel.request.FestivalBgSaveViewModel
import com.theone.mvvm.ext.qmui.showFailTipsDialog
import java.util.*

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-05-12 10:18
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
class FestivalBgAddFragment private constructor():
    BaseFileUploadFragment<Background,FestivalBgAddViewModel, FragmentFestivalBgAddBinding>(),
    DateTimePicker.ResultHandler  {

    init {
        mTableName = "FestivalBg"
    }

    companion object{
        fun newInstance(data:Background? = null): FestivalBgAddFragment {
            return FestivalBgAddFragment().apply {
                data?.let {
                    arguments = Bundle().apply {
                        putParcelable(BundleConstant.DATA,it)
                    }
                }
            }
        }
    }

    private val mPublishVm: FestivalBgSaveViewModel by viewModels()
    private val mFestivalBg:Background? by getValue(BundleConstant.DATA)

    private val mDatePicker: DateTimePicker by lazy {
        DateTimePicker(
            mActivity,
            this,
            getNearestMonthDate(-1),
            getNearestMonthDate(1),
            DateTimePicker.Builder(mActivity).apply {
                setOk("确定")
                setOk("确定")
                setCancel("取消")
                setCancelTextColor(Color.RED)
                setOkTextColor(getColor(mActivity, R.color.qmui_config_color_blue))
                setTitleTextColor(getColor(mActivity, R.color.qmui_config_color_gray_3))
                setKeepLastSelected(true)
                setSelectedTextColor(
                    QMUISkinHelper.getSkinColor(
                        getContentView(),
                        R.attr.app_skin_primary_color
                    )
                )
                setShowType(DateTimePicker.ShowType.DAY)
            })
    }

    override fun getPublishViewModel(): BaseBmobActionViewModel<Background> = mPublishVm

    override fun initData() {
        super.initData()
        mFestivalBg?.let {
            getViewModel().key.set(it.key)
            getViewModel().editable.set(false)
        }
    }

    override fun onPublishClick() {
        if(getViewModel().key.get().isEmpty()){
            showFailTipsDialog("节日名称不能为空")
            return
        }
        super.onPublishClick()
    }

    override fun onPictureNoneSelectClick() {}

    override fun onUploadFileSuccess(files: List<TheFile>) {
        for(file in files){
            file.mFileName = "festival_${getViewModel().date.get()}"
        }
        mFestivalBg.notNull({
            val list = mutableListOf<TheFile>().apply {
                addAll(files)
                addAll(it.getFiles())
            }
            it.attachments = list.toJson()
            mPublishVm.update(it)
        },{
            mPublishVm.save(getViewModel().key.get(),files)
        })
    }

    override fun onAddPictureClick() {
        selectImages()
    }

    override fun handle(date: Date?) {
        date?.let {
            getViewModel().date.set(it.formatString())
        }
    }

    override fun getTitle(): String = "节日壁纸"

    override fun getRecyclerView(): RecyclerView = getDataBinding().recyclerView

    override fun getBindingClick() = ProxyClick()

    inner class ProxyClick{

        fun selectDate(){
            mDatePicker.show(getViewModel().date.get().formatDate())
        }

    }


}